package easy;

public class Solution_953 {

    public boolean isAlienSorted(String[] words, String order) {
        char[] chars = order.toCharArray();
        int[] orders = new int[26];
        for (int i = 0; i < chars.length; i++) {
            orders[chars[i] - 'a'] = i;
        }

        char[] preChars = words[0].toCharArray();
        for (int i = 1; i < words.length; i++) {
            boolean same = true;
            char[] array = words[i].toCharArray();
            int length = Math.min(array.length, preChars.length);
            for (int j = 0; j < length; j++) {
                if (array[j] != preChars[j]) {
                    same = false;
                    if (orders[array[j] - 'a'] > orders[preChars[j] - 'a']) {
                        break;
                    } else {
                        System.out.println(array[j] + "," + preChars[j]);
                        return false;
                    }
                }
            }
            if (same && array.length < preChars.length) {
                return false;
            }
            preChars = array;
        }
        return true;
    }

    public static void main(String[] args) {
        Solution_953 model = new Solution_953();
        System.out.println(model.isAlienSorted(new String[]{"hello", "leetcode"}, "hlabcdefgijkmnopqrstuvwxyz"));
        System.out.println(model.isAlienSorted(new String[]{"word", "world", "row"}, "worldabcefghijkmnpqstuvxyz"));
        System.out.println(model.isAlienSorted(new String[]{"word", "word"}, "worldabcefghijkmnpqstuvxyz"));
        System.out.println(model.isAlienSorted(new String[]{"word", "wordc"}, "worldabcefghijkmnpqstuvxyz"));
        System.out.println(model.isAlienSorted(new String[]{"wordc", "word"}, "worldabcefghijkmnpqstuvxyz"));
    }
}
